package com.twb.wechat.controller;

import com.twb.core.base.Result;
import com.twb.core.base.controller.BaseController;
import com.twb.core.security.annotation.Auth;
import com.twb.core.util.Results;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.validation.constraints.NotBlank;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.service.WxOAuth2Service;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpUserService;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/** 
 * @ClassName WxMpController 
 * @Description 微信服务号 服务接口
 * @author DSY
 * @date 2020年8月27日 下午4:37:17 
 */ 
@RestController
@RequestMapping("/wx/mp/{agentId}")
@Api(value="WxMpController",tags="微信服务号 服务接口")
public class WxMpController extends BaseController{
	@Autowired
	WxMpService wxService;
	
	/**
	 * @Title buildAuthUrl
	 * @Description 构建微信授权地址
	 * @param state
	 * @return
	 * @author DSY
	 * @date 2020年7月1日 下午4:59:58
	 */
	@Auth(verifyToken = false, verifyURL = false)
	@GetMapping("/buildAuthUrl")
	@ApiOperation(value="构建微信授权地址")
	public Result<String> buildAuthUrl(@PathVariable String agentId,
			@RequestParam(name = "state", required = false) String state,
			@RequestParam(name = "redirectUrl", required = false) String redirectUrl) {

		WxMpService mpService = wxService.switchoverTo(agentId);

		String redirectURI = mpService.getWxMpConfigStorage().getOauth2redirectUri();

		if(StringUtils.isNotBlank(redirectUrl)){
			redirectURI = redirectUrl;
		}

		if (StringUtils.isBlank(state)){
			state = "STATE";
		}

		String url = mpService.getOAuth2Service().buildAuthorizationUrl(redirectURI , WxConsts.OAuth2Scope.SNSAPI_USERINFO, state);
		return Results.successWithData(url);
	}
	
	/**
	 * @Title getOpenid
	 * @Description 获取用户微信openid
	 * @param code
	 * @return
	 * @throws WxErrorException
	 * @author DSY
	 * @date 2020年7月2日 上午10:12:07
	 */
	@Auth(verifyToken = false, verifyURL = false)
	@GetMapping("/getOpenid")
	@ApiOperation(value="获取微信授权信息")
	public Result<String> getOpenid(@PathVariable String agentId, @RequestParam(name = "code", required = true) String code) throws WxErrorException {
		WxMpService mpService = wxService.switchoverTo(agentId);

		WxOAuth2AccessToken wxMpOAuth2AccessToken = mpService.getOAuth2Service().getAccessToken(code);

		return Results.successWithData(wxMpOAuth2AccessToken.getOpenId());
	}
	
	/**
	 * @Title getUserInfoByCode
	 * @Description 根据code获取用户信息
	 * @param code
	 * @return
	 * @throws WxErrorException
	 * @author DSY
	 * @date 2020年7月2日 上午10:12:23
	 */
	@Auth(verifyToken = false, verifyURL = false)
	@GetMapping("/getUserInfoByCode")
	@ApiOperation(value="根据code获取用户信息")
	public Result<WxOAuth2UserInfo> getUserInfoByCode(@PathVariable String agentId, @RequestParam(name = "code", required = true) String code) throws WxErrorException {
		WxMpService mpService = wxService.switchoverTo(agentId);
		WxOAuth2Service oAuth2Service = mpService.getOAuth2Service();
		WxOAuth2AccessToken wxMpOAuth2AccessToken = oAuth2Service.getAccessToken(code);
		WxOAuth2UserInfo userInfo = oAuth2Service.getUserInfo(wxMpOAuth2AccessToken, null);
		return Results.successWithData(userInfo);
	}
	
	/**
	 * @Title getUserInfoByOpenid
	 * @Description 根据openid获取用户信息
	 * @param openid
	 * @return
	 * @throws WxErrorException
	 * @author DSY
	 * @date 2020年7月2日 上午10:12:32
	 */
	@Auth(verifyToken = false, verifyURL = false)
	@GetMapping("/getUserInfoByOpenid")
	@ApiOperation(value="根据openid获取用户信息")
	public Result<WxMpUser> getUserInfoByOpenid(@PathVariable String agentId, @RequestParam(name = "openid", required = true) String openid) throws WxErrorException {
		WxMpService mpService = wxService.switchoverTo(agentId);
		WxMpUserService userService = mpService.getUserService();
		WxMpUser userInfo = userService.userInfo(openid);
		return Results.successWithData(userInfo);
	}
	
	/**
	 * @Title getJsapiSignature
	 * @Description 获取jsapi签名信息
	 * @param url
	 * @return
	 * @throws WxErrorException
	 * @author DSY
	 * @date 2020年7月22日 下午5:47:21
	 */
	@Auth(verifyToken = false, verifyURL = false)
	@GetMapping("/getJsapiSignature")
	@ApiOperation(value="获取jsapi签名信息")
	public Result<WxJsapiSignature> getJsapiSignature(
			@PathVariable String agentId,
			@NotBlank(message = "url必填")
			@RequestParam(name = "url", required = true)
			@ApiParam(value="当前网页的URL，不包含#及其后面部分",name="url",required=true)
			String url) throws WxErrorException {
		WxMpService mpService = wxService.switchoverTo(agentId);
		WxJsapiSignature signature = mpService.createJsapiSignature(url );
		mpService.getJsapiTicket();
		return Results.successWithData(signature);
	}
}
